use std::{collections::HashMap, sync::Arc};

use crate::{ServiceComponent, ServiceContextBuilder};

pub struct ServiceContainer {
    pub(crate) id: String,
    pub(crate) name: String,
    pub(crate) stage_tran: i32,
    pub(crate) auto_commit: bool,
    pub(crate) scriptable: bool,
    pub(crate) need_db_connection: bool,
    pub(crate) finish_before_commit: bool,
    pub(crate) property_map: HashMap<String, String>,

    pub(crate) service_context_builder: Arc<dyn ServiceContextBuilder>,
    pub(crate) before_prepare_comp: bool,
    pub(crate) prepare_service: Option<String>,
    pub(crate) prepare_service_component: Option<Arc<dyn ServiceComponent>>,
    pub(crate) process_service_component: Option<Arc<dyn ServiceComponent>>,
    pub(crate) finish_service_component: Option<Arc<dyn ServiceComponent>>,
    pub(crate) finish_service: Option<String>,
}

impl ServiceContainer {
    pub fn new(
        id: String,
        name: String,
        stage_tran: i32,
        auto_commit: bool,
        scriptable: bool,
        need_db_connection: bool,
        finish_before_commit: bool,
        property_map: HashMap<String, String>,
        service_context_builder: Box<dyn ServiceContextBuilder>,
        before_prepare_comp: bool,
        prepare_service: Option<String>,
        prepare_service_component: Option<Box<dyn ServiceComponent>>,
        process_service_component: Option<Box<dyn ServiceComponent>>,
        finish_service_component: Option<Box<dyn ServiceComponent>>,
        finish_service: Option<String>,
    ) -> Self {
        Self {
            id,
            name,
            stage_tran,
            auto_commit,
            scriptable,
            need_db_connection,
            finish_before_commit,
            property_map,
            service_context_builder: Arc::from(service_context_builder),
            before_prepare_comp,
            prepare_service,
            prepare_service_component: prepare_service_component.map(|c| Arc::from(c)),
            process_service_component: process_service_component.map(|c| Arc::from(c)),
            finish_service_component: finish_service_component.map(|c| Arc::from(c)),
            finish_service,
        }
    }
}
